<template>
    <transition>
        <el-dialog :title="dialogType === 'add' ? '添加人才培养方案' : '修改人才培养方案'" v-model="showFlag" width="600px"
            append-to-body @close="handleClose" @open="handleOpen">
            <el-form hide-required-asterisk ref="formRef" :rules="formRule" :model="formInfo" label-width="auto"
                style="padding:30px 30px 0 30px">
                <el-form-item label="年级" prop="grade">
                    <el-input placeholder="年级" type="text" v-model="formInfo.grade"></el-input>
                </el-form-item>
                <el-form-item label="专业" prop="majorId">
                    <el-select placeholder="专业" v-model="formInfo.majorId">
                        <el-option v-for="e in majors" :key="e" :value="e.id" :label="e.name"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item style="margin-top: 40px; margin-left: 372px; ">
                    <el-button @click="handleClose" size="mini">取消</el-button>
                    <el-button @click="formSubmit" size="mini" type="primary">确定</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
    </transition>
</template>

<script setup>
import api from '@/api/api';
import { ElMessage } from 'element-plus';
import { onMounted, reactive, ref, watch } from 'vue';

const props = defineProps({
    dialogType: {
        type: String,
        default: 'add'
    }, showDialog: {
        type: Boolean
    },
    applyId: {
        default: ''
    },
    editForm: {
        type: Object
    }
})
const emit = defineEmits(['dialogClosed'])
const showFlag = ref(false)

const majors = ref([
    // id, name
])

const formRef = ref(null)

const formRule = ref({
    grade: [
        { required: true, message: '请填写年级', trigger: 'blur' }
    ],
    majorId: [
        { required: true, message: '请填写专业', trigger: 'blur' }
    ],
})
const formInfo = reactive(
    {
        'grade': '', 'majorId': '',
    },
)
const formSubmit = () => {
    formRef.value.validate((valid) => {
        if (valid) {
            if (props.dialogType == 'add') {
                api.training_program.add(
                    formInfo.grade, formInfo.majorId
                ).then((res) => {
                    ElMessage({
                        message: '添加成功',
                        type: 'success'
                    })
                }).catch((err) => {
                    console.log(err)
                })

                handleClose()
            } else {
                api.training_program.edit(
                    props.applyId, formInfo.grade, formInfo.majorId
                ).then((res) => {
                    ElMessage({
                        message: '修改成功',
                        type: 'success'
                    })
                }).catch((err) => {
                    console.log(err)
                })

                handleClose()
            }

        }
    })
}
const handleClose = () => {
    showFlag.value = false
    formRef.value.resetFields()
    emit('dialogClosed')
}
const handleOpen = () => {
    if (props.dialogType === 'edit') {
        formInfo.grade = props.editForm.grade
        formInfo.majorId = ''
    }
}

watch(() => props.showDialog, (newValue, oldValue) => {
    showFlag.value = props.showDialog
})


onMounted(() => {
    api.major.get_all().then((res) => {
        majors.value = res.data.data
    }).catch((err) => {
        console.log(err)
    })
})
</script>